﻿using AnalysisEnvironment.PackageProviders;
using AnalysisEnvironment.PackageProviders.Business;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace AnalysisEnvironment
{
    public class LocalNugetPackageProvider : IPackageProvider
    {
        public string NugetPath { get; set; }

        public LocalNugetPackageProvider(string path)
        {
            NugetPath = path;
        }

        //public List<AssemblyName> SearchAssembly(AssemblyName name)
        //{

        //}

        public Assembly TryLoadAssembly(AssemblyName name)
        {
            // 尝试找到对应版本的 目录

            var dirName = Path.Combine(NugetPath, $"{name.Name}.{name.Version}");
            if (Directory.Exists(dirName) != true)
                return null;

            var libPath = Path.Combine(dirName, "lib\\net45");

            var files = Directory.GetFiles(libPath);

            return LocalFilePackageProvider.FindMustMatchFile(name, files?.ToList());
        }
    }
}
